<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../include/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>用户管理页面</title>
<%@ include file="../include/easyui.jsp"%>
<script src="${ctx}/static/plugins/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
</head>
<body class="easyui-layout" style="font-family: '微软雅黑'">
<div data-options="region:'west',split:true,border:false,title:'机构列表'" style="width: 200px">
	<table id="orgDg"></table>
</div>
<div data-options="region:'center',split:true,border:false,title:'用户列表'">
	<div id="toolbar" style="padding:5px;height:auto">
		<div>
			<form id="searchFrom">
				<input type="text" name="username" class="easyui-validatebox" data-options="width:150,prompt: '用户名'"/>
				<input type="text" name="email" class="easyui-validatebox" data-options="width:150,prompt: '邮箱'"/>
				<input type="hidden" id="orgId" name="orgId" value=""/>
				<input type="text" name="createDateStart" class="easyui-my97" datefmt="yyyy-MM-dd" data-options="width:150,prompt: '开始日期'"/>
				- <input type="text" name="createDateEnd" class="easyui-my97" datefmt="yyyy-MM-dd" data-options="width:150,prompt: '结束日期'"/>
				<span class="toolbar-item dialog-tool-separator"></span>
				<a href="javascript(0)" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="query()">查询</a>
			</form>
			<shiro:hasPermission name="system:user:add">
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="add();">添加</a>
				<span class="toolbar-item dialog-tool-separator"></span>
			</shiro:hasPermission>
			<shiro:hasPermission name="system:user:delete">
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" data-options="disabled:false" onclick="del()">删除</a>
				<span class="toolbar-item dialog-tool-separator"></span>
			</shiro:hasPermission>
			<shiro:hasPermission name="system:user:update">
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="update()">修改</a>
				<span class="toolbar-item dialog-tool-separator"></span>
			</shiro:hasPermission>
			<%--<shiro:hasRole name="superAdmin">--%>
			<shiro:hasPermission name="system:user:assignRole">
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-hamburg-suppliers" plain="true" onclick="assignRole()">分配角色</a>
			</shiro:hasPermission>
			<%--</shiro:hasRole>--%>
		</div>
  	</div>
	<table id="dg"></table>
</div>
<div id="dlg"></div>

<script type="text/javascript">
var dg;
var dialog;
var orgDg;
var orgId;
$(function(){
	orgDg=$('#orgDg').treegrid({
		method: "get",
		url:'${ctx}/system/organization/list',
		fit : true,
		fitColumns : true,
		border : false,
		idField : 'id',
		treeField:'name',
		parentField : 'parentId',
		iconCls: 'icon',
		animate:true,
		rownumbers:false,
		singleSelect:true,
		striped:true,
		columns:[[
			{field:'id',title:'id',hidden:true},
			{field:'name',title:'名称',width:80}
		]],
		enableHeaderClickMenu: false,
		enableHeaderContextMenu: false,
		enableRowContextMenu: false,
		dataPlain: true,
		onClickRow:function(rowData){
			orgId=rowData.id;
			$("#orgId").val(orgId);
			dg.datagrid('reload',{orgId:orgId});
		}
	});

	dg=$('#dg').datagrid({
		method: "get",
		url:'${ctx}/system/user/list',
		fit:true,//自动大小
		fitColumns:true,
		border:false,
		idField:'id',
		striped:true,
		pagination:true,//分页控件
		rownumbers:true,//行号
		pageNumber:1,
		pageSize:10,
		pageList:[ 10, 30, 50 ],
		singleSelect:true,//是否单选
		remoteSort:false,//只对本地数据进行排序
		columns:[[
			{field:'id',title:'id',hidden:true},
			{field:'username',title:'用户名',sortable:true,remoteSort:false,width:100},
			{field:'gender',title:'性别',sortable:true,remoteSort:false,width:50,
				formatter : function(value, row, index) {
					return value == 0 ?'男':'女';
				}
			},
			{field:'birthday',title:'出生日期',sortable:true,width:100,formatter:formatterdate},
			{field:'email',title:'电子邮件',sortable:true,width:130},
			{field:'phone',title:'电话',sortable:true,width:100},
			{field:'userType',title:'用户类型',sortable:true,width:100,
				formatter : function(value, row, index) {
					return value == 0 ?'内部用户':'外部用户';
				}},
			{field:'status',title:'状态',sortable:true,width:100,
				formatter : function(value, row, index) {
					return value == 0 ?'有效用户':'无效用户';
				}}
		]],
		headerContextMenu: [
			{
				text: "冻结该列", disabled: function (e, field) { return dg.datagrid("getColumnFields", true).contains(field); },
				handler: function (e, field) { dg.datagrid("freezeColumn", field); }
			},
			{
				text: "取消冻结该列", disabled: function (e, field) { return dg.datagrid("getColumnFields", false).contains(field); },
				handler: function (e, field) { dg.datagrid("unfreezeColumn", field); }
			}
		],
		enableHeaderClickMenu: true,
		enableHeaderContextMenu: true,
		enableRowContextMenu: false,
		toolbar:'#toolbar'
	});
});

//弹窗增加
function add() {
	dialog=$("#dlg").dialog({
	    title: '添加用户',
	    width: 380,
	    height: 380,
		method: "get",
	    href:'${ctx}/system/user/addForm',
	    maximizable:true,
	    modal:true,
	    buttons:[{
			text:'确认',
			handler:function(){
				$("#mainform").submit();
			}
		},{
			text:'取消',
			handler:function(){
				dialog.panel('close');
			}
		}]
	});
}

//删除
function del(){
	var row = dg.datagrid('getSelected');
	if(rowIsNull(row)) return;
	parent.$.messager.confirm('提示', '删除后无法恢复您确定要删除？', function(flag){
		if (flag){
			$.ajax({
				type:'get',
				dateType:'json', //此处也可以不指定,默认就是JSON对象了
				url:"${ctx}/system/user/delete/"+row.id,
				//注意ajax的url的后台action方法必须有返回值return "json"，而不是return null,否则下面的回调函数不起作用，success方法失效
				success:function(data){
					successTip(data.message,dg);
				}
			});
		}
	});
}

//弹窗修改
function update(){
	var row = dg.datagrid('getSelected');
	if(rowIsNull(row)) return;
	dialog=$("#dlg").dialog({
	    title: '修改用户',
	    width: 380,
	    height: 340,
	    href:'${ctx}/system/user/updateForm/'+row.id,
	    maximizable:true,
	    modal:true,
	    buttons:[{
			text:'修改',
			handler:function(){
				$('#mainform').submit();
			}
		},{
			text:'取消',
			handler:function(){
				dialog.panel('close');
			}
		}]
	});
}

//用户分配角色弹窗
function assignRole(){
	var row = dg.datagrid('getSelected');
	if(rowIsNull(row)) return;
	$.ajaxSetup({type : 'GET'});
	dialog=$("#dlg").dialog({
	    title: '分配角色',
	    width: 580,
	    height: 350,
	    href:'${ctx}/system/user/'+row.id+'/assignRoleForm',
	    maximizable:true,
	    modal:true,
	    buttons:[{
			text:'确认',
			handler:function(){
				saveUserRole();
				dialog.panel('close');
			}
		},{
			text:'取消',
			handler:function(){
				dialog.panel('close');
			}
		}]
	});
}

//创建查询对象并查询
function query(){
	var obj=$("#searchFrom").serializeObject();
	dg.datagrid('load',obj);
}

</script>
</body>
</html>